package com.zretc.controller;

import com.github.pagehelper.PageInfo;
import com.zretc.entity.Orders;
import com.zretc.entity.Refund;
import com.zretc.service.BackOrdersService;
import com.zretc.service.BackVipTypeService;
import com.zretc.service.FrontOrdersService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 周文涛
 * 前端用户订单模块
 */
@Controller
@RequestMapping("front")
public class FrontUserOrdersController {
    @Autowired
    private FrontOrdersService frontOrdersService;
    @Autowired
    private BackOrdersService backOrdersService; // 订单列表 订单详情 退票申请列表 退票申请详情
    @Autowired
    private static final Logger LOGGER = LoggerFactory.getLogger(FrontUserOrdersController.class);
    @Autowired
    private BackVipTypeService backVipTypeService;
    // 用户查看订单接口
    @GetMapping("orders")
    @ResponseBody
    public PageInfo<Orders> getUserOrdersList(String ordersId,
                                              String filmName,
                                              String ordersStatus,
                                              @RequestParam("page") String pageNum,
                                              @RequestParam("limit") String pageSize,
                                              @RequestParam("accountId") String accountId){
        // 组装条件
        HashMap<String, String> map = new HashMap<>();
        map.put("ordersId",ordersId);
        map.put("filmName",filmName);
        map.put("ordersStatus",ordersStatus);
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("accountId",accountId);
        PageInfo<Orders> userOrders = backOrdersService.getOrdersList(map);
        return userOrders;

    }

    // 用户查看订单详情接口
    @GetMapping("orders/{id}")
    @ResponseBody
    public Orders getUserOrdersDetails(@PathVariable("id")String ordersId){
        return backOrdersService.getOrders(ordersId);
    }

    //用户查看退单申请接口
    @GetMapping("refund")
    @ResponseBody
    public PageInfo<Refund> getUserRefundList(String ordersId,
                                              String refundId,
                                              String refundStatus,
                                              @RequestParam("page") String pageNum,
                                              @RequestParam("limit") String pageSize,
                                              @RequestParam("accountId") String accountId){

        // 组装条件
        HashMap<String, String> map = new HashMap<>();
        map.put("ordersId",ordersId);
        map.put("refundId",refundId);
        map.put("refundStatus",refundStatus);
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("accountId",accountId);
        return backOrdersService.getRefundList(map);

    }

    //用户查看退单申请详情接口
    @GetMapping("refund/{id}")
    @ResponseBody
    public Refund getUserRefundDetails(@PathVariable("id")String refundId){
        return backOrdersService.getRefund(refundId);
    }


    // 用户选择退票接口
    @PostMapping("refund")
    @ResponseBody
    public boolean createRefund(@RequestParam("ordersId") String ordersId,
                                String refundContent){
        Map<String, String> map = new HashMap<>();
        map.put("ordersId",ordersId);
        map.put("refundContent",refundContent);
        return frontOrdersService.createRefund(map);
    }

    // 用户取消申请退单申请接口
    @DeleteMapping("refund/{id}")
    @ResponseBody
    public boolean cancelRefund(@PathVariable("id") String refundId){
        return frontOrdersService.deleteRefund(refundId);
    }

    // 用户支付
    @GetMapping("pay")
    public String userPay(Model model, String ordersId, HttpServletRequest request){
        model.addAttribute("ordersId",ordersId);
        return "forward:/alipaySum";
    }

    // 用户支付成功
    @GetMapping("paySuccess")
    public String paySuccess(HttpServletRequest request){
        try{
            String ordersId = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "GBK");
            frontOrdersService.paySuccess(ordersId);
            LOGGER.debug("==============用户支付成功，订单状态已修改===========");
        }catch (Exception e){
            e.printStackTrace();
        }

        return "redirect:/front/userOrders.jsp";


    }

    // 选座位
    @GetMapping("seat")
    @ResponseBody
    public Integer[][] getSeat(String screeningId){
        return frontOrdersService.getSeat(screeningId);
    }

    // 创建订单
    @PostMapping("orders")
    @ResponseBody
    public boolean createOrders(String screeningId,
                                String payType,
                                String payPrice,
                                String accountId,
                                String seatArray){
        String[] split = seatArray.split(",");
        HashMap<String, Object> map = new HashMap<>();
        map.put("screeningId",screeningId);
        map.put("payType",payType);
        map.put("payPrice",payPrice);
        map.put("accountId",accountId);
        map.put("seatArray",split);
        return frontOrdersService.createOrders(map);
    }

    // 获取用户折扣
    @GetMapping("userVipType")
    @ResponseBody
    public Float getUserVipType(String accountId){
        return backVipTypeService.findDiscountById(Integer.valueOf(accountId));
    }

}
